<template>
  <div>
    <el-dialog title="修改库位" :visible.sync="updateDepartvis" @close="close" width="400px" append-to-body>
        <el-form :rules="rules" :model="updateDepartForm" ref="updateDepart">

            <el-form-item prop="departmentCode" label="编号:" :label-width="formLabelWidth">
                <el-input v-model="updateDepartForm.departmentCode"></el-input>
            </el-form-item>
            <el-form-item label="备注:" :label-width="formLabelWidth">
                <el-input v-model="updateDepartForm.departmentRemark"></el-input>
            </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="close">取 消</el-button>
          <el-button type="danger" :loading="submitLoading" @click="submitDepart('updateDepart')">确 定</el-button>
        </div>
          </el-dialog>
        </div>
</template>

<script>
import { AddStoreArea, UpdateDepart } from '@/api/store.js';

export default {
  name: 'addstore',
  props: ['updateDepartdialog', 'checkDepart'],
  data() {
    return {
      updateDepartvis: false,
      submitLoading: false,
      updateDepartForm: {
        departmentCode: '',
        departmentRemark: '',
      },
      formLabelWidth: '80px',
      rules: {
          departmentCode: [
            { required: true, message: '请输入库位编号', trigger: 'blur' },
          ],
      }
    }
  },
  computed: {
    main_store() {
      return this.$store.state.store.main_store;
    },
    store_id() {
      return this.$store.state.store.store_id;
    },
  },
  methods: {
    close() {
      this.updateDepartvis = false;
      this.resetForm();
      this.$emit("update:updateDepartdialog", false);
    },
    resetForm() {
      this.updateDepartForm.departmentCode = '';
      this.updateDepartForm.departmentRemark = '';
    },
    submitDepart(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.submitLoading = true;     
          let reqData2 = {
              areaId: this.checkDepart.areaId,
              departmentCode: this.updateDepartForm.departmentCode,
              departmentId: this.checkDepart.departmentId,
              departmentRemark: this.updateDepartForm.departmentRemark,
          }
          // 更新库位
          UpdateDepart(reqData2).then(response => {
              let data = response.data
              this.$message({
                  message: data.msg,
                  type: 'success'
              })
              this.submitLoading = false;
              this.$refs.updateDepart.resetFields();
              this.resetForm();
              this.updateDepartvis = false;
              this.$emit("update:updateDepartdialog", false);
          }).catch(error => {
              this.submitLoading = false;
          })
          
        } else {
          console.log('error submit!!');
          return false;
        }
      });
      
    }
  },
  watch: {
    updateDepartdialog(newVal,oldVal) {  
      this.updateDepartvis = newVal;
    },
    checkDepart(newVal) {
       if (newVal) {
        this.updateDepartForm.departmentCode = this.checkDepart.departmentCode;
        this.updateDepartForm.departmentRemark = this.checkDepart.departmentRemark;
        }
    }
  }
}
</script>

<style lang="scss" scoped>
  
</style>